package com.ideafactory.client.views;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ideafactory.client.beans.CatalogGoodsBean;
import com.ideafactory.client.utils.CommonUtils;
import com.ideafactory.client.utils.TextVUtils;
import com.ideafactory.client.ybservice.APP;
import com.ideafactory.client.ybselfservice.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class BottomViewAdapter extends RecyclerView.Adapter<BottomViewAdapter.BottomHolder> {
    private Context context;
    private int mCurrentSelectPosition = -1;
    private List<CatalogGoodsBean> catalogGoodsLists;

    public BottomViewAdapter(Context context) {
        this.context = context;
        catalogGoodsLists = new ArrayList<>();
    }

    public void setCatalogGoodsLists(List<CatalogGoodsBean> catalogGoodsLists) {
        this.catalogGoodsLists = catalogGoodsLists;
    }

    @Override
    public BottomHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_view_item, parent, false);
        return new BottomHolder(view);
    }

    @Override
    public void onBindViewHolder(BottomHolder holder, final int position) {
        holder.setData(holder, position);
    }

    @Override
    public int getItemCount() {
        return catalogGoodsLists.size();
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        MyFocusView bottom_goods_des;
        TextView bottom_goods_money;
        ImageView cancle_delete;

        public BottomHolder(View itemView) {
            super(itemView);
            bottom_goods_des = (MyFocusView) itemView.findViewById(R.id.bottom_goods_des);
            bottom_goods_money = (TextView) itemView.findViewById(R.id.bottom_goods_money);
            cancle_delete = (ImageView) itemView.findViewById(R.id.bottom_btn_cancle);
        }

        public void setData(final BottomHolder holder, final int position) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCurrentSelectPosition = position;
                    dismisPop();
                    showBottomPop(holder.itemView, position);
                    notifyDataSetChanged();
                }
            });

            CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);

            bottom_goods_des.setText(catalogGoodsBean.getName() + " X" + catalogGoodsBean.getGoodsAdded()+"");
            bottom_goods_money.setText(context.getResources().getString(R.string.money_unit)+CommonUtils.mulNum(catalogGoodsBean.getPrice().doubleValue(), catalogGoodsBean.getGoodsAdded().intValue()));
            cancle_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//删除单个文件
                    deleteSingleItem(position);
                }
            });

            if (position == mCurrentSelectPosition) {
                itemView.setBackgroundResource(R.mipmap.bottom_view_click);
            } else {
                itemView.setBackgroundResource(R.mipmap.bottom_view_normal);
            }
        }
    }

    /**
     * 点击红叉删除单个文件
     */
    private void deleteSingleItem(int position) {
        CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);
        int goodsAddCount = catalogGoodsBean.getGoodsAdded();
        for (int i = 0; i < goodsAddCount; i++) {
            sendMsg(SUB_MONEY,catalogGoodsBean.getPrice());
        }
        catalogGoodsLists.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();//删除后位置有变
        if (catalogGoodsLists.size() == 0) {
            sendMsg(CLEAR_MONEY, 0d);
        }
    }

    public void addData(CatalogGoodsBean catalogGoodsBean) {
        boolean isHas = false;
        int goodsCount = catalogGoodsLists.size();
        for (int i = 0; i < goodsCount; i++) {
            CatalogGoodsBean catalogGoodsBean1 = catalogGoodsLists.get(i);
            if (catalogGoodsBean1.getGoodsId().equals(catalogGoodsBean.getGoodsId())) {
                    //如果此商品被添加到的数量达到商品数量的最大值
                if(catalogGoodsBean.getCount()!=-1 && catalogGoodsBean.getCount()<=catalogGoodsBean1.getGoodsAdded()){
                    sendMsg(INVALID_MONEY, 0d);
                }else{
                    catalogGoodsBean1.setGoodsAdded(catalogGoodsBean1.getGoodsAdded() + 1);

                    sendMsg(ADD_MONEY,catalogGoodsBean1.getPrice());
                    Log.e("已添加", catalogGoodsBean1.getGoodsAdded() + "");
                    catalogGoodsLists.remove(i);
                    catalogGoodsLists.add(i, catalogGoodsBean1);
                    notifyItemChanged(i);
                }
                isHas = true;
                break;
            }
        }
        if (!isHas) {
            catalogGoodsLists.add(catalogGoodsBean);
            sendMsg(ADD_MONEY,catalogGoodsBean.getPrice());
            notifyDataSetChanged();
        }
    }


    /**
     * 判断是否有库存
     */
    public boolean getGoodsAdded(String goodsId){
        int goodsCount = catalogGoodsLists.size();
        for (int i = 0; i < goodsCount; i++) {
            CatalogGoodsBean catalogGoodsBean1 = catalogGoodsLists.get(i);
            if (catalogGoodsBean1.getGoodsId().equals(goodsId)) {
                if(catalogGoodsBean1.getGoodsAdded()<catalogGoodsBean1.getCount()||catalogGoodsBean1.getCount()==-1){
                    return true;
                }else{
                    return false;
                }
            }
        }
        return true;
    }

    public List<CatalogGoodsBean> getBottomData() {
        return catalogGoodsLists;
    }
    public void clearBottom() {
        Log.d("底部清理","清理");
        catalogGoodsLists.clear();
        notifyDataSetChanged();
    }

    /**
     * 加载底部弹出框
     */
    private PopupWindowHelper popupWindowHelper;

    private void showBottomPop(View baseView, final int position) {

        View view = LayoutInflater.from(context).inflate(R.layout.bottom_popup, null);
        popupWindowHelper = new PopupWindowHelper(view, 350, 250);
        final ImageView popImage = (ImageView) view.findViewById(R.id.bottom_pop_view_item_image);
        TextView tv_good_name = (TextView) view.findViewById(R.id.bottom_pop_view_name);
        final TextView bottom_view_money = (TextView) view.findViewById(R.id.bottom_view_money);
        Button bottom_pop_good_sub = (Button) view.findViewById(R.id.bottom_pop_good_sub);
        Button bottom_pop_goods_add = (Button) view.findViewById(R.id.bottom_pop_goods_add);
        final TextView bottom_pop_goods_count = (TextView) view.findViewById(R.id.bottom_pop_goods_count);
        final TextView bottom_pop_money_left = (TextView) view.findViewById(R.id.bottom_pop_money_left);
        CatalogGoodsBean catalogpop = catalogGoodsLists.get(position);
        ImageLoader.getInstance().displayImage(catalogpop.getImageSrc().get(0), popImage, APP.getAPP().getImageOption());

        tv_good_name.setText(catalogpop.getName());
        bottom_view_money.setText(TextVUtils.highlight(context.getResources().getString(R.string.money_unit) + catalogpop.getPrice()));
        bottom_pop_goods_count.setText(catalogpop.getGoodsAdded() + "");
        bottom_pop_money_left.setText(TextVUtils.highlight(""+CommonUtils.mulNum(catalogpop.getPrice(), catalogpop.getGoodsAdded())));
        bottom_pop_good_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);
                if (catalogGoodsBean.getGoodsAdded() > 1) {

                    sendMsg(SUB_MONEY, catalogGoodsBean.getPrice());

                    int size = catalogGoodsBean.getGoodsAdded() - 1;
                    catalogGoodsLists.get(position).setGoodsAdded(size);

                    if (size == 0) {
                        deleteSingleItem(position);
                        popupWindowHelper.dismiss();
                        mCurrentSelectPosition = -1;
                    } else {
                        bottom_pop_goods_count.setText(size + "");
                        bottom_pop_money_left.setText(TextVUtils.highlight(CommonUtils.mulNum(catalogGoodsBean.getPrice(), catalogGoodsLists.get(position).getGoodsAdded()) + ""));
                    }
                    notifyDataSetChanged();
                }
            }
        });

        bottom_pop_goods_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CatalogGoodsBean catalogGoodsBean = catalogGoodsLists.get(position);
                if(catalogGoodsBean.getGoodsAdded()<catalogGoodsBean.getCount() ||catalogGoodsBean.getCount()==-1){
                    sendMsg(ADD_MONEY,catalogGoodsBean.getPrice());
                    int size = catalogGoodsBean.getGoodsAdded() + 1;
                    catalogGoodsLists.get(position).setGoodsAdded(size);
                    bottom_pop_goods_count.setText(size + "");
                    bottom_pop_money_left.setText(TextVUtils.highlight(CommonUtils.mulNum(catalogGoodsBean.getPrice(),catalogGoodsBean.getGoodsAdded())  + ""));
                    notifyDataSetChanged();
                }else{
                    sendMsg(INVALID_MONEY, 0d);
                }
            }
        });

        popupWindowHelper.setOnDisMisListener(new PopupWindowHelper.onDisMisListener() {
            @Override
            public void Dismissed() {
                mCurrentSelectPosition = -1;
                notifyDataSetChanged();
            }
        });
        popupWindowHelper.showAsPopUp(baseView, 0, 69);
    }

    public OnreceiveBottomChange onreceiveBottomChange;
    public void setOnreceiveBottomChange(OnreceiveBottomChange onreceiveBottomChange) {
        this.onreceiveBottomChange = onreceiveBottomChange;
    }

    //popupwindow 上面更新数据信息
    public interface OnreceiveBottomChange {
        void onAddMoney(double money);
        void onSubMoney(double money);
        void bottomCleared();//底部无数据状态
        void itemAddInvalid();
    }

    private final int ADD_MONEY =1;
    private final int SUB_MONEY =2;
    private final int CLEAR_MONEY =3;
    private final int INVALID_MONEY =4;
    private void sendMsg(int id, Double money){
        Log.e("money操作","id"+id+ "钱"+money);
        switch (id){
            case ADD_MONEY:
                if (onreceiveBottomChange != null) {
                    onreceiveBottomChange.onAddMoney(money.doubleValue());
                }
                break;
            case SUB_MONEY:
                if (onreceiveBottomChange != null) {
                    onreceiveBottomChange.onSubMoney(money.doubleValue());
                }
                break;
            case CLEAR_MONEY:
                if (onreceiveBottomChange != null) {
                    onreceiveBottomChange.bottomCleared();
                }
                break;
            case INVALID_MONEY:
                if (onreceiveBottomChange != null) {
                    onreceiveBottomChange.itemAddInvalid();
                }
                break;
        }
    }

    public void dismisPop() {
        if (popupWindowHelper != null) {
            popupWindowHelper.dismiss();
        }
    }

}
